const GeoJSON = {}

GeoJSON.ID_FIELD = "FID" // default field name of imported *JSON feature ids

GeoJSON.typeLookup = {
  LineString: 'polyline',
  MultiLineString: 'polyline',
  Polygon: 'polygon',
  MultiPolygon: 'polygon',
  Point: 'point',
  MultiPoint: 'point'
}

GeoJSON.translateGeoJSONType = function(type) {
  return GeoJSON.typeLookup[type] || null
}

GeoJSON.pathIsRing = function(coords) {
  const first = coords[0]
  const last = coords[coords.length - 1]
  // TODO: consider detecting collapsed rings
  return coords.length >= 4 && first[0] == last[0] && first[1] == last[1]
}
GeoJSON.importGeometry = function(geom, importer, opts) {
  const type = geom.type
  if (type in GeoJSON.pathImporters) {
    if (opts.geometry_type && opts.geometry_type != GeoJSON.translateGeoJSONType(type)) {
      // kludge to filter out all but one type of geometry
      return
    }
    GeoJSON.pathImporters[type](geom.coordinates, importer)
  } else if (type == 'GeometryCollection') {
    geom.geometries.forEach((geom) => {
      GeoJSON.importGeometry(geom, importer, opts)
    })
  } else {
    // verbose("GeoJSON.importGeometry() Unsupported geometry type:", geom.type)
  }
}
// Functions for importing geometry coordinates using a PathImporter
//
GeoJSON.pathImporters = {
  LineString: function(coords, importer) {
    importer.importLine(coords)
  },
  MultiLineString: function(coords, importer) {
    for (let i=0; i<coords.length; i++) {
      GeoJSON.pathImporters.LineString(coords[i], importer)
    }
  },
  Polygon: function(coords, importer) {
    for (let i=0; i<coords.length; i++) {
      importer.importRing(coords[i], i > 0)
    }
  },
  MultiPolygon: function(coords, importer) {
    for (let i=0; i<coords.length; i++) {
      GeoJSON.pathImporters.Polygon(coords[i], importer)
    }
  },
  Point: function(coord, importer) {
    importer.importPoints([coord])
  },
  MultiPoint: function(coords, importer) {
    importer.importPoints(coords)
  }
}
export default GeoJSON